package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.transformation.editor.model.GraphicalDomain;
import edu.pku.sei.transformation.editor.model.pattern.INodeTemplate;

public class NodeTemplateCreateCommand extends Command {
	private GraphicalDomain domain;
	private INodeTemplate template;

	public NodeTemplateCreateCommand(INodeTemplate template,
			GraphicalDomain domain) {
		this.domain = domain;
		this.template = template;
	}

	@Override
	public boolean canExecute() {
		if (this.domain != null && this.template != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void execute() {
		// while ( !domain.getOwner().registerName(variable.getIdentifier(),
		// variable)) {
		// variable.setIdentifier(variable.getIdentifier()+"_r");
		// }
		domain.getPattern().getNodes().add(template);
		template.setPattern(domain.getPattern());
		domain.bindNewVariable2Exp();
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void undo() {
		domain.getPattern().getNodes().remove(template);
		template.setPattern(null);
	}
}
